package com.engc.common.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

/**
 * 用户角色授权
 * 
 * @author guzhixiong
 * 
 */
@Controller
@RequestMapping("/sysUserRole")
public class Sys_User_Role {
	private BaseService baseService;

	@RequestMapping("/view")
	public String view() {
		return "system/sysuserrole/SysUserRole";
	}

	@RequestMapping("/getUserRole")
	@ResponseBody
	public Map<String, Object> getUserRole(HttpServletRequest req) {
		return baseService.getGridData("select * from (select a.*,b.name as rolename,b.description from sys_user_role a,sys_role b where a.role_id = b.id ) c",
				"c", req);
	}

	/**
	 * 保存
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> insert(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		record.put("id", baseService.getSeqValue("seq_sys_user_role"));
		try {
			baseService.Insert("sys_user_role", record);
			return BaseUtils.getSuccessMessage("角色授权新增成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("角色授权新增失败,系统发生异常");
		}
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getUpdateParam(request, "id");
		try {
			baseService.Update("sys_user_role", record, "id=?", request.getParameter("id"));
			return BaseUtils.getSuccessMessage("角色授权修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("角色授权修改失败,系统发生异常");
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> Delete(HttpServletRequest request) {
		try {
			baseService.Delete("sys_user_role", "id=?", request.getParameter("id"));
			return BaseUtils.getSuccessMessage("角色授权删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("角色授权删除失败，系统发生异常");
		}
	}

	@Resource
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
